package cn.haocxy.jfm.gui;

import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.Group;
import javafx.scene.Node;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Border;
import javafx.scene.layout.BorderStroke;
import javafx.scene.layout.BorderStrokeStyle;
import javafx.scene.layout.BorderWidths;
import javafx.scene.layout.CornerRadii;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.scene.text.TextAlignment;
import cn.haocxy.jfm.file.Clipboard;
import cn.haocxy.jfm.file.FileIcons;
import cn.haocxy.jfm.func.FileFunc;
import cn.haocxy.jfm.util.Executor;

public class NameArea {

	private static final double TEXT_WRAPPING_WIDTH = FileIcons.ICONSIZE * 1.6;
	
	private final FileItemGUI fileItemGUI;
	
	private Group root = new Group();
	
	private boolean inEdit = false;
	
	private Text text = new Text();
	
	private TextField textField = null;
	
	final EventHandler<MouseEvent> onClickOut = new EventHandler<MouseEvent>() {

		@Override
		public void handle(MouseEvent e) {
			if (!getTextField().isHover()) {
				save();
			}
		}
		
	};
	
	public NameArea(FileItemGUI fileItemGUI) {
		this.fileItemGUI = fileItemGUI;
		
		text.setWrappingWidth(TEXT_WRAPPING_WIDTH);
		text.setTextAlignment(TextAlignment.CENTER);
		text.setText(fileItemGUI.getModel().toFilePath().getFileName());
		root.getChildren().add(text);
		
	}
	
	private void normal() {
		if (!inEdit) {
			return;
		}
		inEdit = false;
		root.getChildren().clear();
		root.getChildren().add(text);
		fileItemGUI.getFileListPane().toNode().requestFocus();
		
		toNode().getScene().removeEventHandler(MouseEvent.MOUSE_CLICKED, onClickOut);
	}
	
	/**
	 * 切换到编辑状态
	 */
	public void edit() {
		if (inEdit) {
			return;
		}
		inEdit = true;
		root.getChildren().clear();
		root.getChildren().add(getTextField());
		
		TextField tf = getTextField();
		tf.setText(text.getText());
		tf.requestFocus();
		
		// 选中扩展名前面的部分
		String name = tf.getText();
		int i = name.indexOf('.');
		if (i == -1) {
			tf.selectAll();
		} else {
			tf.selectRange(0, i);
		}
		
		toNode().getScene().addEventHandler(MouseEvent.MOUSE_CLICKED, onClickOut);
		
	}
	
	private void rename(String newName) {
		FileFunc.rename(fileItemGUI.getModel().toFilePath(), newName, (succeed, ex)->{
			Executor.exeui(()->{
				if (ex != null) {
					fileItemGUI.getFileListPane().info(ex.getMessage());
				}
				if (succeed) {
					Clipboard.get().clear();
				}
			});
		});
	}
	
	private void save() {
		try {
			rename(textField.getText());
		} catch (Exception ex) {
			// 暂时什么都不做, 仅仅是防止normal()不被执行
		}
		normal();
	}
	
	private void cancel() {
		normal();
	}
	
	public Node toNode() {
		return root;
	}
	
	private TextField getTextField() {
		if (textField == null) {
			textField = new TextField();
			initTextArea();
		}
		return textField;
	}
	
	private void initTextArea() {
		textField.setMaxWidth(TEXT_WRAPPING_WIDTH);
		
		BackgroundFill fill = new BackgroundFill(Color.WHITE, CornerRadii.EMPTY, Insets.EMPTY);
		Background bg = new Background(fill);
		textField.setBackground(bg);
		
		BorderWidths bw = new BorderWidths(1);
		BorderStroke bs = new BorderStroke(Color.BLUE, BorderStrokeStyle.SOLID, CornerRadii.EMPTY, bw);
		Border bd = new Border(bs);
		textField.setBorder(bd);
		
		textField.setAlignment(Pos.CENTER);
		
		textField.setOnKeyReleased(e->{
			KeyCode code = e.getCode();
			if (code.equals(KeyCode.ENTER)) {
				save();
				return;
			}
			if (code.equals(KeyCode.ESCAPE)) {
				cancel();
				return;
			}
		});
	}
}
